package com.test.ssm.controller;

import com.test.ssm.pojo.Account;
import com.test.ssm.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
//@RequestMapping("shop")
public class AccountController {
    @Autowired
    private AccountService accountService;

    @RequestMapping("first.html")
    public String first() {
        return "WEB-INF/pages/login.jsp";
    }

    @RequestMapping("login.html")
    public String login(Account account, HttpSession session, Map map) {
        System.out.println(account.getName());
        Account ac = accountService.getPasswordByName(account.getName());
        if (ac != null) {
            if (ac.getPassword().equals(account.getPassword())) {
                session.setAttribute("account", ac);
                return "redirect:index.html";
            } else {
                map.put("message", "密码输入不正确，请重新输入");
                return "WEB-INF/pages/login.jsp";
            }
        } else {
            map.put("message", "密码输入不正确，请重新输入");
            return "WEB-INF/pages/login.jsp";
        }

    }

    @RequestMapping("destory.html")
    public String destoryAccount(HttpSession session) {
        session.invalidate();
        return "WEB-INF/pages/login.jsp";
    }


}
